package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.product.domain.Category;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-02-24
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public List<Category> treeSelect(Long parentId) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, parentId);
        List<Category> categoryList = baseMapper.selectList(queryWrapper);

        if(!CollectionUtils.isEmpty(categoryList)){
            categoryList.forEach(category -> {
                // 判断是否有子节点
                Long count = baseMapper.selectCount(new LambdaQueryWrapper<Category>().eq(Category::getParentId, category.getId()));
                category.setHasChildren(count > 0);
            });
        }
        return categoryList;
    }

    @Override
    public List<CategoryVo> getLevelOneCategory() {
        //查询所有一级分类
        List<Category> categoryList = baseMapper.selectList(new LambdaQueryWrapper<Category>().eq(Category::getParentId, 0));
        //封装成vo
        return categoryList.stream().map(category -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category, categoryVo);
            return categoryVo;
        }).toList();
    }

    @Override
    public List<CategoryVo> tree() {
        // 查询所有分类 并根据一级分类查子分类 再一直往下查 直到没有子分类
        List<Category> categoryList = baseMapper.selectList(null);
        // 封装成voList
        List<CategoryVo> categoryVoList = categoryList.stream().map(category -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category, categoryVo);
            return categoryVo;
        }).toList();
        return nestedTree(categoryVoList);
    }

    private List<CategoryVo> nestedTree(List<CategoryVo> categoryVoList) {
        // 构建 parentId -> 子分类列表的映射
        Map<Long, List<CategoryVo>> map = new HashMap<>();
        for (CategoryVo categoryVo : categoryVoList) {
            Long parentId = categoryVo.getParentId();
            map.computeIfAbsent(parentId, k -> new ArrayList<>()).add(categoryVo);
        }
        /*
        Map<Long, List<CategoryVo>> map = new HashMap<>();
        for (CategoryVo categoryVo : categoryVoList) {
            Long parentId = categoryVo.getParentId();

            // 检查 map 中是否已经存在 parentId 对应的列表
            if (!map.containsKey(parentId)) {
                // 如果不存在，则创建一个新的 ArrayList 并放入 map 中
                map.put(parentId, new ArrayList<>());
            }

            // 获取 parentId 对应的列表，并将当前 categoryVo 添加到列表中
            List<CategoryVo> childrenList = map.get(parentId);
            childrenList.add(categoryVo);
        }
        */

        // 查找所有根节点并递归构建树
        List<CategoryVo> trees = new ArrayList<>();
        for (CategoryVo categoryVo : categoryVoList) {
            if (categoryVo.getParentId() == 0) { // 根节点
                buildTree(categoryVo, map);
                trees.add(categoryVo);
            }
        }
        return trees;
    }

    private void buildTree(CategoryVo categoryVo, Map<Long, List<CategoryVo>> map) {
        List<CategoryVo> children = map.get(categoryVo.getId());
        if (children != null) {
            categoryVo.setChildren(children);
            for (CategoryVo child : children) {
                buildTree(child, map); // 递归构建子树
            }
        }
    }
}
